package cn.lg.soar.system.biz.modules.message.controller;

import cn.lg.soar.common.util.current.ICurrentUser;
import cn.lg.soar.common.util.current.UserContext;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.biz.modules.message.service.impl.SseServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * @author luguoxiang 469599794@qq.com
 * @Date: 2023/11/22 22:50
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Api(tags = "Sse控制器")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/sse")
public class SseController {

    private final SseServiceImpl manager;

    public SseController(SseServiceImpl manager) {
        this.manager = manager;
    }

    @GetMapping(value = "open", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter open() {
        ICurrentUser user = UserContext.getUser();
        return manager.open(user.getId(), user.getTokenId());
    }

    @GetMapping (value = "close")
    public void close(){
        ICurrentUser user = UserContext.getUser();
        manager.close(user.getTokenId());
    }

}
